﻿using mars_client.csharp;
using mars_client.csharp.cache;
using mars_client.csharp.enums;
using mars_client.csharp.model;
using mars_client.csharp.model.vo;
using mars_client.csharp.radar;
using mars_client.csharp.util;
using mars_client.csharp.vo;
using mars_client.gui.window;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace mars_client.gui.page
{
    /// <summary>
    /// MyFriends.xaml 的交互逻辑
    /// </summary>
    public partial class MyFriendsPage : Page
    {
        GuiCache guic = GuiCache.Instance;
        UserCache uc = UserCache.Instance;
        Radar r = Radar.Instance;
        public MyFriendsPage()
        {
            InitializeComponent();
        }
        public void search(object sender, MouseButtonEventArgs e)
        {
            MessageCO co = new MessageCO();
            co.type = (int)RequestEnum.request.FindUser;
            UserVO userVO = new UserVO();
            userVO.username = seachBox.Text;
            co.content = userVO;
            Radar r = Radar.Instance;
            r.sendData(co);
        }
        /// <summary>
        /// 点击朋友，显示对话列表和对话信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void MineFriendStackPanel_MouseUp(object sender, MouseButtonEventArgs e)
        {
            ChatListPage chatPage = (ChatListPage)guic.getGui("ChatListPage");
            MainWindow mw = (MainWindow)guic.getGui("MainWindow");
            StackPanel sp = (StackPanel)sender;
            Label label = (Label)sp.Children[1];
            mw.Dispatcher.InvokeAsync(() =>
            {
                mw.LeftFrame.Content = chatPage;
                mw.changeBtnStyle("chat");
                chatPage.Dispatcher.InvokeAsync(() =>
                {
                    // 判断是否相同
                    List<Grid> grids = new List<Grid>();
                    bool isExist = false;
                    foreach (Grid grid in chatPage.listWrapper.Children)
                    {
                        if (grid.Uid.Equals(sp.Uid))
                        {
                            isExist = true;
                            break;
                        }
                    }
                    if (!isExist)
                    {
                        ChatListItemVO vo = new ChatListItemVO();
                        vo.id = sp.Uid;
                        vo.name = label.Content.ToString();
                        vo.type = "_SingleChatPage";
                        vo.headImg = ((Image)sp.Children[0]).Source.ToString();
                        chatPage.listWrapper.Children.Add(RenderUtil.generateChatListItemTemp(vo));
                    }
                });
            });
        }

        private void arrow_MouseUp(object sender, MouseButtonEventArgs e)
        {
            AddFriendWindow addFriendWindow = new AddFriendWindow();
            guic.setGui("AddFriendWindow",addFriendWindow);
            addFriendWindow.Show();
        }
        public void deleleFriend(object sender, MouseButtonEventArgs e)
        {
            Image img = (Image)sender;
            MessageCO messageCO = new MessageCO();
            messageCO.type = (int)RequestEnum.request.DeleteFriend;
            UserVO userVO = new UserVO();
            userVO.id = long.Parse(img.Uid.ToString());
            messageCO.content = userVO;

            Radar.Instance.sendData(messageCO);
        }
    }
}
